<?php

require_once 'libs/smarty/Smarty.class.php';

class Renderer
{
    /**
     * @var string
     */
    private $templatesRoot;

    private $simpleTestMode;

    /**
     * @param string $templatesRoot
     */
    public function __construct($templatesRoot, $simpleTestMode = false)
    {
        $this->templatesRoot = $templatesRoot;
        $this->simpleTestMode = $simpleTestMode;
    }

    /**
     * @return Smarty
     */
    private function CreateSmartyObject()
    {
        $result = new Smarty();
        $result->template_dir = $this->simpleTestMode ?
                (dirname(dirname(__FILE__)) . '/' . $this->templatesRoot) :
                $this->templatesRoot;            
        return $result;
    }

    /**
     * @param string $templateName
     * @param array $vars
     * @return string
     */
    public function Render($templateName, $vars)
    {
        $smarty = $this->CreateSmartyObject();
        foreach ($vars as $name => $value)
            $smarty->assign($name, $value);
        return $smarty->fetch(
            (
                $this->simpleTestMode ?
                    dirname(dirname(__FILE__)) . '/' . $this->templatesRoot . '/'
                    : ''
            ) . $templateName . '.tpl');
    }
}

?>